package org.datastealer.dao.api;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：用户自定义sqlDAO层接口
 * @date ：Created in 2020/9/21
 */
public interface CustomDao {

    /**
     * 执行自定义sql
     *
     * @param sql
     * @return
     */
    Object runCustomSql(String sql);

    /**
     * 从ResultSet获取返回结果
     *
     * @param resultSet
     * @return
     * @throws SQLException
     */
    default List<Map<String, Object>> convertListMap(ResultSet resultSet) throws SQLException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        Map<String, Object> rowData;
        while (resultSet.next()) {
            rowData = new HashMap<String, Object>(16);
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(metaData.getColumnName(i), resultSet.getObject(i));
            }
            list.add(rowData);
        }
        resultSet.close();
        return list;
    }

}